﻿@{
    ViewBag.Title = ViewData["Action"].ToString() + " Group - Project Management System";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewData["Action"].ToString() Group</h2>
<form id="form" action="" method="post">
<div class="content-box" id="Group">
    <!-- From Content Box -->
    <div class="content-box-header">
        <h3>
            Group Info</h3>
        <div class="clear">
        </div>
    </div>
    <!-- To .content-box-header -->
    <div class="content-box-content">
        @{ if (TempData["Message"] != null)
           {
            <div class="notification png_bg" id="messageBox">
                <a href="#" class="close">
                    <img src="../../Content/images/icons/cross_grey_small.png" title="Close this notification"
                        alt="close" /></a>
                <div>
                    @TempData["Message"].ToString()
                </div>
            </div>
           }
        }
        <fieldset>
            <p>
                <input type="hidden" id="Action" name="Action" value="@ViewData["Action"]" />
                <input type="hidden" id="txtGroupID" name="GroupID" value="@Model.GroupID" />
                <input type="hidden" id="txtStatus" name="Status" value="@Model.Status" />
                <input type="hidden" id="txtOwnerID" name="OwnerID" value="@Model.OwnerID" />
                <label class="width12 floatLeft">
                    Group Name:</label>
                <input type="text" id="txtGroupName" name="GroupName" class="width20 input-inline text-input medium-input" value="@Model.GroupName"/>
            </p>
            <br clear="all" />
            <h3>
                Members:</h3>
            <br clear="all" />
            <div id="Members">
                @{if (ViewData["Action"].ToString() == PMS.ENT.ActionName.Insert.ToString())
                  {
                    <fieldset id="Members1">
                        <div id="Member1">
                            <p>
                                @Html.DropDownList("ListGroupMember[0].UserID", (SelectList)ViewData["UserList"], new { @class = "width20 input-inline Member" })
                                @*User tạo nhóm , mặc định *@ @*<a href="#" onclick="RemoveMember('Members1')">
                                    <img src="../../Content/images/cancel_48.gif" alt="" />
                                </a>*@
                            </p>
                        </div>
                    </fieldset> 
                    
                    <div style="height: 20px">
                    </div>
                  }
                  else
                  {
                      for (int count = 0; count < Model.ListGroupMember.Count; count++)
                      {
                    <fieldset id="Members@(count)" style="border-radius: 6px 6px 6px 6px;
                    @(Model.ListGroupMember[count].Status == "Confirm" ?
                    "background-color:#CCFFCC;border:1px solid #9ADF8F;" :
                                        (Model.ListGroupMember[count].Status == "Decline"
                                        ? "background-color:#FFCCCC;border:1px solid #CC9999;" :
                                        "background-color:#FFFF99;border:1px solid #FFCC99")) ">
                        <div id="Member@(count)">
                            <p>
                                @Html.DropDownList("ListGroupMember[" + count + "].UserID", (SelectList)ViewData["UserList" + count], new { @class = "width20 input-inline Member", @disabled = "disabled" })
                                <input type="hidden" id="UserGroupID@(count)" name="ListGroupMember[@(count)].UserGroupID" value="@Model.ListGroupMember[count].UserGroupID" />
                                <input type="hidden" id="Status@(count)" name="ListGroupMember[@(count)].Status" value="@Model.ListGroupMember[count].Status" />
                                <input type="hidden" id="GroupID@(count)" name="ListGroupMember[@(count)].GroupID" value="@Model.ListGroupMember[count].GroupID" />
                                <input type="hidden" id="UserID@(count)" name="ListGroupMember[@(count)].UserID" value="@Model.ListGroupMember[count].UserID" />
                                @{if (Model.Status == "Draft")
                                  {

                                      if (Model.ListGroupMember[count].Status == "Waiting for Confirm")
                                      {
                                          if (PMS.Utilities.Utility.UserID == Model.ListGroupMember[count].UserID)
                                          {
                                    <a href="#" onclick="ApplyGroup('@Model.ListGroupMember[count].UserGroupID','@(count)')">
                                        <img alt="" style="text-align:center;width:22px"  src="@Url.Content("~/Content/images/icons/Actions-user-group-new-icon.png")" title="Apply" />
                                    </a>
                                    <a href="#" onclick="DeclineGroup('@Model.ListGroupMember[count].UserGroupID','@(count)')">
                                        <img alt="" style="text-align:center;width:22px" src="@Url.Content("~/Content/images/icons/Actions-user-group-delete-icon.png")" title="Decline" />
                                    </a>
                                          }
                                      }

                                      if (PMS.Utilities.Utility.UserID == Model.OwnerID
                                          && ViewData["Action"].ToString() == PMS.ENT.ActionName.Update.ToString())
                                      {
                                          if (Model.ListGroupMember[count].UserID != Model.OwnerID)
                                          {    
                                    <a href="#" onclick="DeleteUserGroup('@Model.ListGroupMember[count].UserGroupID','@(count)')">
                                        <img alt="" style="text-align:center;width:22px" src="@Url.Content("~/Content/images/icons/Button-Close-icon.png")" title="Delete" />
                                    </a>
                                          }

                                      }
                                  }
                                   
                                }

                                <span style="float: right; padding: 5px; font-weight: bolder;">
                                    @Html.CheckBox("ListGroupMember[" + count + "].IsUploadFile", (bool)Model.ListGroupMember[count].IsUploadFile)
                                    Upload file
                                    @Html.CheckBox("ListGroupMember[" + count + "].IsDeleteFile", (bool)Model.ListGroupMember[count].IsDeleteFile)
                                    Delete file </span><span style="float: left; padding: 5px; font-weight: bolder; width: 150px">
                                        @(Model.ListGroupMember[count].Status)
                                    </span>
                            </p>
                        </div>
                    </fieldset>
                    <div style="height: 20px">
                    </div>
                      }
                  }
                }
            </div>
            @{if (ViewData["Action"].ToString() != PMS.ENT.ActionName.View.ToString()
                                      && Model.Status == "Draft"
                                      )
              {
                <p>
                    <a href="#" onclick="AddMember()">
                        <img src="../../Content/images/add_48.gif" />Add new member</a>
                </p>
              }
            }
            <p>
                @{if (ViewData["Action"].ToString() != PMS.ENT.ActionName.View.ToString())
                  {
                      //store current url
                    <input type="hidden" name="url" id="url" />
                    <input class="button" type="submit" value="Submit" id="btnSubmit" />
                  }
                }
                <input class="button" type="button" value="Back to list" id="btnBack" onclick="DoUrl('@Url.Action("ManageGroup", "Project")')" />
            </p>
        </fieldset>
    </div>
</div>
</form>
<script type="text/javascript">
    $("#url").val(document.URL);
    $("#Group").addClass("current");
    $("#Project").addClass("current");
    var action = document.getElementById("Action").value;
    var memberCount = 0;
    if (action == 'Insert') {
        memberCount = 1;
    //User mac dinh , khong thay doi cho leader 
        $("#ListGroupMember_0__UserID").change(function(){
            this.value= @PMS.Utilities.Utility.UserID;
            alert("sorry , you can't change leader member");
        })
    }
    else {
        memberCount = @Model.ListGroupMember.Count;
    }
    function AddMember() {
        memberCount = memberCount + 1;
        var html = '<fieldset id="Members' + memberCount + '">\
                        <div id="Member' + memberCount + '">\
                            <p>';
        
        html = html + '<select class="width20 input-inline Member" id="ListGroupMember_counter__UserID" name="ListGroupMember[counter].UserID">';
                        @foreach (var item in (SelectList)ViewData["UserList"])
                        {
                         <text> html = html + '<option value="@item.Value">@item.Text</option>'; </text>
                        } 
        html = html + '</select>';
        html = html + ' <a href="#" val="Members' + memberCount + '" onclick=RemoveMember("Members' + memberCount + '")> <img src="../../Content/images/cancel_48.gif" alt="" /></a>\
                            </p>\
                        </div>\
                    </fieldset> <div style="height:20px"></div>';
                    
        html = html.replace('counter', (memberCount - 1));
        html = html.replace('[counter]', '[' + (memberCount - 1)+']');
        $("#Members").append(html);
    }

    function RemoveMember(id) {
        $('#' + id).remove();

    }

    function ApplyGroup(usergroupid,count)
    {
        $.post('@Url.Action("ConfirmUserGroup", "Project")', { usergroupID: usergroupid, status:'Confirm' }, function (data) {
                if (data != null) {
                    alert("Success Join Group");
                    location.reload();
                }
            });
    }

    function DeclineGroup(usergroupid,count)
    {
        $.post('@Url.Action("ConfirmUserGroup", "Project")', { usergroupID: usergroupid, status:'Decline' }, function (data) {
                if (data != null) {
                   alert("Success Decline Group");
                   location.reload();
                }
            });
    }

    function DeleteUserGroup(usergroupid,count)
    {
        $.post('@Url.Action("DeleteUserGroup", "Project")', { usergroupID: usergroupid }, function (data) {
                if (data != null) {
                   alert("Success Delete");
                   location.reload();
                }
            });
    }
    

    $('.Member').live('change', function () {
        var value = $(this).val();
        if (value != 0) {
            var id = $(this).attr('id');
            $(".Member").each(function () {
                if ($(this).attr('id') != id && $(this).val() == value) {
                    jAlert('Member already in group!', 'Message');
                    $("#" + id).val('');
                }
            });

        }
    });

    if(@Html.Raw(Json.Encode(ViewData["Action"])) == "View")
    {
        $(".input-inline").attr("disabled","disabled");
        $(".textarea").attr("disabled","disabled");
    }
</script>
